<?php

session_start();
load("@.other");//加载other函数文件
load("@.webservice");//加载other函数文件
load("@.weixin");//加载other函数文件

//用于判断区分PC设备和移动设备
function isMobile() {  
	$useragent=isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
	$useragent_commentsblock=preg_match('|\(.*?\)|',$useragent,$matches)>0?$matches[0]:'';

	function CheckSubstrs($substrs,$text){
		foreach($substrs as $substr)
			if(false!==strpos($text,$substr)){
				return true;
			}
		return false;
	}
	
	$mobile_os_list=array('Google Wireless Transcoder','Windows CE','WindowsCE','Symbian','Android','armv6l','armv5','Mobile','CentOS','mowser','AvantGo','Opera Mobi','J2ME/MIDP','Smartphone','Go.Web','Palm','iPAQ');
	$mobile_token_list=array('Profile/MIDP','Configuration/CLDC-','160×160','176×220','240×240','240×320','320×240','UP.Browser','UP.Link','SymbianOS','PalmOS','PocketPC','SonyEricsson','Nokia','BlackBerry','Vodafone','BenQ','Novarra-Vision','Iris','NetFront','HTC_','Xda_','SAMSUNG-SGH','Wapaka','DoCoMo','iPhone','iPod','iPad');
	
	$found_mobile=CheckSubstrs($mobile_os_list,$useragent_commentsblock) || CheckSubstrs($mobile_token_list,$useragent);
	
	if ($found_mobile){
		return true;
	}
	else{
		return false;
	}
}

//邮箱格式判断
function isEmail($email) {
	$result = preg_match("/^([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)$/",$email);
	return $result?true:false;
}

//手机号码判断
function isTel($tel) {
	$result = preg_match("/^13[0-9]{1}[0-9]{8}$|15[0-9]{1}[0-9]{8}$|18[0-9]{1}[0-9]{8}$/",$tel);
	return $result?true:false;
}

//字串格式转化
function stringformat($str , $intype = "" , $outtype = ""){
	if(!in_array($intype , array('GBK' , 'UTF-8' , 'GB2312')) || !in_array($outtype , array('GBK' , 'UTF-8' , 'GB2312'))) {
		return $str;
	}
	if(empty($str)) {
		return $str;
	}
	if(is_array($str)){
		foreach ($str as $key=>$value){
			if(is_array($str[$key])){
				$str[$key] = stringformat($str[$key] , $intype , $outtype);
			}else{
				$str[$key] = iconv($intype , $outtype."//IGNORE" , $str[$key]);
			}
		}
	} else {
		$str = iconv($intype , $outtype , $str);
	}
	return $str;
}

//将2个相同类型的数组连接起来
function array_join($first,$second) {
	$result =array();
	foreach($first as $k=>$v) {
		$result[] = $v;
	}
	foreach($second as $k=>$v) {
		$result[] = $v;
	}
	return $result;
}

//跳转到前一个页面
function redirect_history() {
	$url = $_SERVER['HTTP_REFERER'];
	if($url!="") {
		header('location:'.$url);
	}
}

//转义问题
function all_stripslashes($shuru) {
	//判断是否有转义
	$result = "";
	$magic_quote = get_magic_quotes_gpc();
	if($magic_quote) {
		//去除转义
		if(!empty($shuru)) {
			if(is_array($shuru)) {//数组
				$result = array();
				foreach($shuru as $k=>$v) {
					if(is_array($v)) {
						$result[$k] = all_stripslashes($v);
					}else{
						$result[$k] = stripslashes($v);
					}
				}
			}else{//字符串
				$result = stripslashes($shuru);
			}
		}else{//输入为空
			$result = $shuru;
		}
	}else{
		$result = $shuru;
	}

	return $result;
}

//获的图片上传路径的连接符
function getfuhao() {
	$pos = strpos(C('PIC_ROOT'), "/");
	if($pos===false) {
		$fuhao = "\\";
	} else {
		$fuhao = "/";
	}
	return $fuhao;
}

//删除文件
function delfile($weizhi,$name) {
	$fuhao = getfuhao();
	$path = C('PIC_ROOT').$fuhao.$weizhi.$fuhao.$name;
	
	if (file_exists($path)) {
		$result = unlink ($path);
		return true;
	} else {
		return false;
	}
}

//删除文件夹
function deldir($weizhi) {
	$fuhao = getfuhao();
	$path = C('PIC_ROOT').$fuhao.$weizhi;

	//先删除目录下的文件
	$dir = opendir($path);
	while (($file = readdir($dir))!=false) {
		if($file!="." && $file!="..") {
			$fullpath = $path.$fuhao.$file;
			if(!is_dir($fullpath)) {
				unlink($fullpath);
			} else {
				deldir($fullpath);
			}
		}
	}

	closedir($dir);

	//删除当前文件夹
	if(rmdir($path)) {
		return true;
	} else {
		return false;
	}
}

//object转数组
function object_array($array) {
	if(is_object($array)) {
		$array = (array)$array;
	} if(is_array($array)) {
		foreach($array as $key=>$value) {
			$array[$key] = object_array($value);
		}
	}
	return $array;
}

//打印
function dayin($canshu) {
	echo "<br/><pre>";
	print_r($canshu);
	echo "<pre>";
}


?>